package root.electronclass.electronclass.application;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import root.electronclass.electronclass.common.base.BasicAdapter;
import root.electronclass.electronclass.common.base.BindingHolder;
import root.electronclass.electronclass.common.base.ViewBindingFragment;
import root.electronclass.electronclass.R;
import root.electronclass.electronclass.databinding.FragmentApplicationBinding;
import root.electronclass.electronclass.databinding.ItemApplicationBinding;
import root.electronclass.electronclass.grade.model.PersonLoginResult;
import root.electronclass.electronclass.grade.model.PersonType;
import root.electronclass.electronclass.other.AttestationActivity;
import root.electronclass.electronclass.other.WebFragment;
import root.electronclass.electronclass.mvp.GlobalParameter;
import root.electronclass.electronclass.mvp.base.BaseResponse;
import root.electronclass.electronclass.mvp.rest.RestManager;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ApplicationFragment extends ViewBindingFragment<FragmentApplicationBinding> {

    private String url;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initAdapter();
    }

    private void initAdapter() {
        List<ApplicationData> list = new ArrayList<>();
//        ApplicationData data2 = new ApplicationData("校车申请", "内部应用", R.mipmap.icon_xcsq, WebFragment.APPLICATION_SCHOOLBUS);
//        ApplicationData data3 = new ApplicationData("留校申请", "内部应用", R.mipmap.icon_lxsq, WebFragment.APPLICATION_STAYINSCHOOL);
//        ApplicationData data4 = new ApplicationData("心理咨询预约", "内部应用", R.mipmap.icon_xlzx, WebFragment.APPLICATION_PSYCHOLOGICAL);
//        ApplicationData data5 = new ApplicationData("设备报修", "内部应用", R.mipmap.sbbx, WebFragment.DEVICEMANAGEMENT);
//        ApplicationData data6 = new ApplicationData("学生请假", "内部应用", R.mipmap.xsqj, WebFragment.LEAVE);
//        ApplicationData data7 = new ApplicationData("考场模式", "内部应用", R.mipmap.kcms, WebFragment.EXAMINATIONROOM);
        ApplicationData data8 = new ApplicationData("智慧食堂", "内部应用", R.mipmap.zhst, WebFragment.NEWCANTEEN);
//        ApplicationData data9 = new ApplicationData("请假总览", "内部应用", R.mipmap.qjzl, WebFragment.LEAVESTATS);
//        ApplicationData data10 = new ApplicationData("综合素质评价", "内部应用", R.mipmap.icon_zhszpj, WebFragment.ZHSZPJ);
//        list.add(data2);
//        list.add(data3);
//        list.add(data4);
//        list.add(data5);
//        list.add(data6);
//        list.add(data7);
        list.add(data8);
//        list.add(data9);
//        list.add(data10);
        getBinding().recycler.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        BasicAdapter adapter = new BasicAdapter<ApplicationData, ItemApplicationBinding>() {

            @Override
            public void onBindViewHolder(@NonNull BindingHolder<ItemApplicationBinding> holder, @Nullable ApplicationData item) {
                holder.getBinding().name.setText(item.name);
                holder.getBinding().type.setText(item.type);
                holder.getBinding().image.setActualImageResource(item.image);
                holder.getBinding().item.setOnClickListener(v -> {
                    if (item.name.equals("智慧食堂")) {
                        toast("电子班牌-食堂业务暂未开通");
                        return;
                    }
                    if (item.name.equals("考场模式")) {
                        WebFragment.startWeb(getActivity(), item.url);
                        return;
                    }

                    if (item.name.equals("综合素质评价")) {
                        deviceLogin(item.url);
                        return;
                    }

                    url = item.url;
                    startActivityForResult(new Intent(getActivity(), AttestationActivity.class), AttestationActivity.getAPPLICATION());
                });
            }
        };
        getBinding().recycler.setAdapter(adapter);
        adapter.notifyChanged(list);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == AttestationActivity.getAPPLICATION()) {
            boolean isSuccess = data.getBooleanExtra("SUCCESS", false);
            if (isSuccess) {

                if (url.equals(WebFragment.LEAVESTATS)) {
                    RestManager.getInstance().initRest().loginUser().enqueue(new Callback<BaseResponse<PersonLoginResult>>() {
                        @Override
                        public void onResponse(Call<BaseResponse<PersonLoginResult>> call, Response<BaseResponse<PersonLoginResult>> response) {
                            if (response.isSuccessful() && response.body().getRows().getPersonType() == PersonType.TEACHER) {
                                WebFragment.startWeb(getActivity(), url);
                            } else {
                                toast("无权限");
                            }
                        }

                        @Override
                        public void onFailure(Call<BaseResponse<PersonLoginResult>> call, Throwable t) {

                        }
                    });

                } else {
                    WebFragment.startWeb(getActivity(), url);
                }

            }
        }
    }

    private void deviceLogin(String url) {
        String deviceID = GlobalParameter.getDeviceCode();
        RestManager.getInstance().initRest().deviceLogin(deviceID)
                .enqueue(new Callback<BaseResponse<String>>() {
                    @Override
                    public void onResponse(Call<BaseResponse<String>> call, Response<BaseResponse<String>> response) {
                        if (response.isSuccessful()) {
                            RestManager.getInstance().setTOKEN(response.body().getRows());
                            WebFragment.startWeb(getActivity(), url);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseResponse<String>> call, Throwable t) {

                    }
                });
    }
}